package leetcode.editor.cn.q1_300.q50;

/**
 * @author qidi
 * @date 2021-12-10 11:17:41
 */
class Q0058_LengthOfLastWord {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int lengthOfLastWord(String s) {
            int length = 0;
            int i = s.length() - 1;
            while (i >= 0) {
                if (s.charAt(i) == ' ') {
                    if (length != 0) {
                        return length;
                    }
                } else {
                    length++;
                }
                i--;
            }
            return length;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0058_LengthOfLastWord().new Solution();
        System.out.println(solution.lengthOfLastWord("Hello World"));
        System.out.println(solution.lengthOfLastWord("   fly me   to   the moon  "));
        System.out.println(solution.lengthOfLastWord("luffy is still joyboy"));
    }
}